package com.imooc.business.util.report;

import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
public class SchoolYearUtil {
    private static final String UNKNOWN_YEAR = "未知年份";
    private static final String UNKNOWN_SEASON = "未知季度";
    private static final String SEASON_NAMES = "学年第一学期|学年第二学期|春季|秋季|年春季|年秋季";
    private static final String SPRING_SEASON = "春季";
    public static final String SECOND_SCHOOL_YEAR = "学年第二学期";
    public static final String SPRING_TYPE = "1";
    private static final String FALL_SEASON = "秋季";
    public static final String FIRST_SCHOOL_YEAR = "学年第一学期";
    private static final String FALL_TYPE = "2";
    private static final String SPRING_FIRSTDAY = "-01-08";
    private static final String SPRING_LASTDAY = "-08-31";
    private static final String FALL_FIRSTDAY = "-09-01";
    private static final String FALL_LASTDAY = "-01-07";

    public SchoolYearUtil() {
    }

    public static Date getSpringStartDate(Date date) {
        String year = getSchoolYear(date);
        Date beginDate = DateUtil.parse(Integer.parseInt(year) + 1 + "-01-08", "yyyy-MM-dd");
        return DateUtil.parse(LunarCalendarUtil.lunarToSolar(beginDate), "yyyy-MM-dd");
    }

    public static String getSchoolYearSeason(String year_) {
        return !year_.contains("秋季") && !year_.contains("春季") && !year_.contains("学年第一学期") && !year_.contains("学年第二学期") ? year_ + "学年第一学期" : year_;
    }

    public static Date getSpringEndDate(Date date) {
        String year = getSchoolYear(date);
        return DateUtil.parse(Integer.parseInt(year) + 1 + "-08-31", "yyyy-MM-dd");
    }

    public static String getSeasonStartDate(Date date) {
        String year = getSchoolYear(date);
        return "1".equals(getSeasonType(date)) ? DateUtil.format(getSpringStartDate(date), "yyyy-MM-dd") : String.valueOf(year).concat("-09-01");
    }

    public static String getSeasonEndDate(Date date) {
        String year = getSchoolYear(date);
        return "1".equals(getSeasonType(date)) ? String.valueOf(Integer.parseInt(year) + 1).concat("-08-31") : DateUtil.format(getSpringStartDate(date), "yyyy-MM-dd");
    }

    public static Date getSeasonStartDate(String seasonName) {
        String year = seasonName.replaceAll("学年第一学期|学年第二学期|春季|秋季|年春季|年秋季", "");
        if (year.indexOf("-") >= 0) {
            year = year.split("-")[0];
        }

        String date = null;
        if (!seasonName.contains("秋季") && !seasonName.contains("学年第一学期")) {
            date = Integer.valueOf(year) + 1 + "-01-08";
            return DateUtil.parse(LunarCalendarUtil.lunarToSolar(DateUtil.parse(date, "yyyy-MM-dd")), "yyyy-MM-dd");
        } else {
            date = year + "-09-01";
            return DateUtil.parse(date, "yyyy-MM-dd");
        }
    }

    public static Date getSeasonEndDate(String seasonName) {
        String year = seasonName.replaceAll("学年第一学期|学年第二学期|春季|秋季|年春季|年秋季", "");
        if (year.indexOf("-") >= 0) {
            year = year.split("-")[0];
        }

        String date = null;
        if (!seasonName.contains("秋季") && !seasonName.contains("学年第一学期")) {
            date = Integer.valueOf(year) + 1 + "-08-31";
            return DateUtil.parse(date, "yyyy-MM-dd");
        } else {
            date = Integer.valueOf(year) + 1 + "-01-07";
            return DateUtil.parse(LunarCalendarUtil.lunarToSolar(DateUtil.parse(date, "yyyy-MM-dd")), "yyyy-MM-dd");
        }
    }

    public static String getSeasonName(Date date) {
        if (date == null) {
            return "未知季度";
        } else {
            return "1".equals(getSeasonType(date)) ? "春季" : "秋季";
        }
    }

    public static String getSeasonType(Date date) {
        Date springStartDate = getSpringStartDate(date);
        Date springEndDate = getSpringEndDate(date);
        return (!date.after(springStartDate) || !date.before(springEndDate)) && !DateUtil.format(date, "yyyy-MM-dd").equals(DateUtil.format(springStartDate, "yyyy-MM-dd")) && !DateUtil.format(date, "yyyy-MM-dd").equals(DateUtil.format(springEndDate, "yyyy-MM-dd")) ? "2" : "1";
    }

    public static String getSchoolYear(Date date) {
        if (date == null) {
            return "未知年份";
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int month = cal.get(2) + 1;
            if (month < 9) {
                cal.add(1, -1);
            }

            return String.valueOf(cal.get(1));
        }
    }

    public static String getSchoolYearName(Date date) {
        if (date == null) {
            return null;
        } else {
            String year = getSchoolYear(date);
            int iYear = Integer.parseInt(year);
            String yearName = iYear + "-" + (iYear + 1);
            return StringUtils.equals("1", getSeasonType(date)) ? yearName + "学年第二学期" : yearName + "学年第一学期";
        }
    }

    public static String getTermYearName(Date date) {
        if (date == null) {
            return null;
        } else {
            String year = getSchoolYear(date);
            int iYear = Integer.parseInt(year);
            String yearName = iYear + "-" + (iYear + 1);
            return StringUtils.equals("1", getSeasonType(date)) ? yearName + "学年" : yearName + "学年";
        }
    }

    public static String getTermName(Date date) {
        if (date == null) {
            return null;
        } else {
            return StringUtils.equals("1", getSeasonType(date)) ? "下学期" : "上学期";
        }
    }

    public static String getSchoolYear(String date) {
        if (date == null) {
            return "未知年份";
        } else {
            Date dt = DateUtil.parse(date, "yyyy-MM-dd");
            String year = getSchoolYear(dt);
            int iYear = Integer.parseInt(year);
            String yearName = iYear + "-" + (iYear + 1);
            return yearName;
        }
    }

    public static String getSchoolYearName(String date) {
        return date == null ? null : getSchoolYearName(DateUtil.parse(date, "yyyy-MM-dd"));
    }

    public static Date getStartDate(String year) {
        Date date = DateUtil.parse(year.concat("-09-01"), "yyyy-MM-dd");
        return date;
    }

    public static Date getPlatformStartDate() {
        return DateUtil.parseDate("20130101");
    }

    public static Date getEndDate(String year) {
        return DateUtil.parse(String.valueOf(Integer.parseInt(year) + 1).concat("-08-31"), "yyyy-MM-dd");
    }
}
